package com.cmc.cloud.cmclink.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.lock.annotation.Lock4j;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cmc.cloud.cmclink.doc.api.document.emums.PortTypeEnum;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.PortSpecialRequestErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.entity.PortSpecialRequestDO;
import com.cmc.cloud.cmclink.doc.mapper.PortSpecialRequestMapper;
import com.cmc.cloud.cmclink.doc.service.PortSpecialRequestService;
import com.cmc.cloud.cmclink.doc.vo.portspecialrequestvo.PortSpecialRequestCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.portspecialrequestvo.PortSpecialRequestPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.portspecialrequestvo.PortSpecialRequestUpdateReqVO;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageParam;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 接货地或目的地单证特殊要求配置 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class PortSpecialRequestServiceImpl implements PortSpecialRequestService {

    @Resource
    private PortSpecialRequestMapper portSpecialRequestMapper;

    @Override
    @Lock4j(name = "portSpecialRequestUnique", keys = {"#createReqVO.getPortType", "#createReqVO.getPortCode", "#createReqVO.getRequestCode"})
    public void createPortSpecialRequest(PortSpecialRequestCreateReqVO createReqVO) {
        // 插入
        validateUniqueExists(createReqVO.getPortType(), createReqVO.getPortCode(), createReqVO.getRequestCode(), null);
        PortSpecialRequestDO portSpecialRequest = BeanUtil.copyProperties(createReqVO, PortSpecialRequestDO.class);
        portSpecialRequestMapper.insert(portSpecialRequest);
    }

    public void validateUniqueExists(String portType, String portCode, String requestCode, Long id) {
        LambdaQueryWrapperX<PortSpecialRequestDO> queryWrapperX = new LambdaQueryWrapperX<PortSpecialRequestDO>()
                .eqIfPresent(PortSpecialRequestDO::getPortType, portType)
                .eqIfPresent(PortSpecialRequestDO::getPortCode, portCode)
                .eqIfPresent(PortSpecialRequestDO::getRequestCode, requestCode);
        if (id != null) {
            queryWrapperX.ne(PortSpecialRequestDO::getId, id);
        }
        if (portSpecialRequestMapper.selectCount(queryWrapperX) != 0L) {
            throw ServiceExceptionUtil.exception(PortSpecialRequestErrorCodeConstants.PORT_SPECIAL_REQUEST_EXISTS);
        }
    }

    @Override
    @Lock4j(name = "portSpecialRequestUnique", keys = {"#updateReqVO.getPortType", "#updateReqVO.getPortCode", "#updateReqVO.getRequestCode"})
    public void updatePortSpecialRequest(PortSpecialRequestUpdateReqVO updateReqVO) {
        // 校验存在
        validatePortSpecialRequestExists(updateReqVO.getId());
        validateUniqueExists(updateReqVO.getPortType(), updateReqVO.getPortCode(), updateReqVO.getRequestCode(), updateReqVO.getId());
        // 更新
        PortSpecialRequestDO updateObj = BeanUtil.copyProperties(updateReqVO, PortSpecialRequestDO.class);
        portSpecialRequestMapper.updateById(updateObj);
    }

    @Override
    public void deletePortSpecialRequest(Long id) {
        // 校验存在
        validatePortSpecialRequestExists(id);
        // 删除
        portSpecialRequestMapper.deleteById(id);
    }

    private void validatePortSpecialRequestExists(Long id) {
        if (portSpecialRequestMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(PortSpecialRequestErrorCodeConstants.PORT_SPECIAL_REQUEST_NOT_EXISTS);
        }
    }

    @Override
    public PortSpecialRequestDO getPortSpecialRequest(Long id) {
        return portSpecialRequestMapper.selectById(id);
    }

    @Override
    public List<PortSpecialRequestDO> getPortSpecialRequestList(Collection<Long> ids) {
        return portSpecialRequestMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PortSpecialRequestDO> getPortSpecialRequestPage(PortSpecialRequestPageReqVO reqVO) {
        IPage<PortSpecialRequestDO> page = Page.of(reqVO.getPageNo(), reqVO.getPageSize());
        LambdaQueryWrapperX<PortSpecialRequestDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.eqIfPresent(PortSpecialRequestDO::getPortCode, reqVO.getPortCode());
        return portSpecialRequestMapper.selectPage((PageParam) reqVO, queryWrapperX);
    }

    @Override
    public List<PortSpecialRequestDO> filterPortSpecialRequestList(String porCode, String delCode) {
        LambdaQueryWrapperX<PortSpecialRequestDO> queryWrapperX = new LambdaQueryWrapperX<PortSpecialRequestDO>().eq(PortSpecialRequestDO::getPortType, PortTypeEnum.POR)
                .eq(PortSpecialRequestDO::getPortCode, porCode);
        queryWrapperX.or(sonWrapperX -> sonWrapperX.eq(PortSpecialRequestDO::getPortType, PortTypeEnum.DEL)
                .eq(PortSpecialRequestDO::getPortCode, delCode));
        return portSpecialRequestMapper.selectList(queryWrapperX);
    }
}
